<template>
  <div class="api-test-page">
    <div class="page-header">
      <h1>API 接口测试</h1>
      <p>测试各种地图和天气相关的API接口</p>
    </div>
    
    <!-- API模式切换 -->
    <div class="api-mode-switch">
      <div class="mode-toggle">
        <button 
          @click="useMockApi = false" 
          :class="{ active: !useMockApi }"
          class="mode-btn"
        >
          <i class="fa fa-globe"></i>
          真实API
        </button>
        <button 
          @click="useMockApi = true" 
          :class="{ active: useMockApi }"
          class="mode-btn"
        >
          <i class="fa fa-database"></i>
          模拟API
        </button>
      </div>
      <div class="mode-info">
        <span v-if="useMockApi" class="mock-info">
          <i class="fa fa-info-circle"></i>
          当前使用模拟数据，无需网络连接
        </span>
        <span v-else class="real-info">
          <i class="fa fa-wifi"></i>
          当前使用真实API，需要网络连接
        </span>
      </div>
    </div>
    
    <div class="api-container">
      <!-- 天气API测试 -->
      <div class="api-section">
        <h2><i class="fa fa-cloud"></i> 天气API</h2>
        <div class="api-form">
          <div class="input-group">
            <label>城市名称:</label>
            <input v-model="weatherForm.city" placeholder="如：北京、上海、广州" />
          </div>
          <div class="input-group">
            <label>坐标位置:</label>
            <input v-model="weatherForm.location" placeholder="如：116.397428,39.90923" />
          </div>
          <button @click="testWeatherApi" :disabled="loading" class="test-btn">
            <i class="fa fa-search"></i>
            {{ loading ? '请求中...' : '获取天气' }}
          </button>
        </div>
        <div v-if="weatherResult" class="api-result">
          <h3>天气信息</h3>
          <pre>{{ JSON.stringify(weatherResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 地理编码API测试 -->
      <div class="api-section">
        <h2><i class="fa fa-map-marker"></i> 地理编码API</h2>
        <div class="api-form">
          <div class="input-group">
            <label>编码类型:</label>
            <select v-model="geocodeForm.type">
              <option value="geo">地址转坐标</option>
              <option value="regeo">坐标转地址</option>
            </select>
          </div>
          <div v-if="geocodeForm.type === 'geo'" class="input-group">
            <label>地址:</label>
            <input v-model="geocodeForm.address" placeholder="如：北京市朝阳区" />
          </div>
          <div v-if="geocodeForm.type === 'regeo'" class="input-group">
            <label>坐标:</label>
            <input v-model="geocodeForm.location" placeholder="如：116.397428,39.90923" />
          </div>
          <button @click="testGeocodeApi" :disabled="loading" class="test-btn">
            <i class="fa fa-map"></i>
            {{ loading ? '请求中...' : '地理编码' }}
          </button>
        </div>
        <div v-if="geocodeResult" class="api-result">
          <h3>地理编码结果</h3>
          <pre>{{ JSON.stringify(geocodeResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- POI搜索API测试 -->
      <div class="api-section">
        <h2><i class="fa fa-search"></i> POI搜索API</h2>
        <div class="api-form">
          <div class="input-group">
            <label>中心坐标:</label>
            <input v-model="poiForm.location" placeholder="如：116.397428,39.90923" />
          </div>
          <div class="input-group">
            <label>关键词:</label>
            <input v-model="poiForm.keywords" placeholder="如：餐厅、酒店、银行" />
          </div>
          <div class="input-group">
            <label>搜索半径(米):</label>
            <input v-model="poiForm.radius" type="number" placeholder="3000" />
          </div>
          <div class="input-group">
            <label>类型:</label>
            <input v-model="poiForm.types" placeholder="如：餐饮服务、住宿服务" />
          </div>
          <button @click="testPoiApi" :disabled="loading" class="test-btn">
            <i class="fa fa-location-arrow"></i>
            {{ loading ? '请求中...' : '搜索POI' }}
          </button>
        </div>
        <div v-if="poiResult" class="api-result">
          <h3>POI搜索结果</h3>
          <pre>{{ JSON.stringify(poiResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 路径规划API测试 -->
      <div class="api-section">
        <h2><i class="fa fa-route"></i> 路径规划API</h2>
        <div class="api-form">
          <div class="input-group">
            <label>起点坐标:</label>
            <input v-model="routeForm.origin" placeholder="如：116.397428,39.90923" />
          </div>
          <div class="input-group">
            <label>终点坐标:</label>
            <input v-model="routeForm.destination" placeholder="如：116.407428,39.91923" />
          </div>
          <div class="input-group">
            <label>路径策略:</label>
            <select v-model="routeForm.strategy">
              <option value="0">速度优先</option>
              <option value="1">费用优先</option>
              <option value="2">距离优先</option>
              <option value="3">不走高速</option>
              <option value="4">躲避拥堵</option>
              <option value="5">多策略</option>
            </select>
          </div>
          <div class="input-group">
            <label>途经点:</label>
            <input v-model="routeForm.waypoints" placeholder="如：116.402428,39.91423" />
          </div>
          <button @click="testRouteApi" :disabled="loading" class="test-btn">
            <i class="fa fa-directions"></i>
            {{ loading ? '请求中...' : '路径规划' }}
          </button>
        </div>
        <div v-if="routeResult" class="api-result">
          <h3>路径规划结果</h3>
          <pre>{{ JSON.stringify(routeResult, null, 2) }}</pre>
        </div>
      </div>

      <!-- 快速测试按钮 -->
      <div class="quick-test-section">
        <h2><i class="fa fa-bolt"></i> 快速测试</h2>
        <div class="quick-buttons">
          <button @click="quickTestWeather" class="quick-btn">
            <i class="fa fa-cloud"></i>
            测试北京天气
          </button>
          <button @click="quickTestGeocode" class="quick-btn">
            <i class="fa fa-map-marker"></i>
            测试坐标转地址
          </button>
          <button @click="quickTestPoi" class="quick-btn">
            <i class="fa fa-search"></i>
            测试POI搜索
          </button>
          <button @click="quickTestRoute" class="quick-btn">
            <i class="fa fa-route"></i>
            测试路径规划
          </button>
        </div>
      </div>

      <!-- 错误信息显示 -->
      <div v-if="error" class="error-message">
        <i class="fa fa-exclamation-triangle"></i>
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

// 使用API composable
const { 
  getWeather, getGeocode, getPoi, getRoute,
  getWeatherMock, getGeocodeMock, getPoiMock, getRouteMock 
} = useApi();

// 响应式数据
const loading = ref(false);
const error = ref('');
const useMockApi = ref(true); // 默认使用模拟API

// 表单数据
const weatherForm = ref({
  city: '',
  location: ''
});

const geocodeForm = ref({
  type: 'regeo' as 'geo' | 'regeo',
  address: '',
  location: ''
});

const poiForm = ref({
  location: '',
  keywords: '',
  radius: 3000,
  types: ''
});

const routeForm = ref({
  origin: '',
  destination: '',
  strategy: 0,
  waypoints: ''
});

// 结果数据
const weatherResult = ref<any>(null);
const geocodeResult = ref<any>(null);
const poiResult = ref<any>(null);
const routeResult = ref<any>(null);

// 测试天气API
const testWeatherApi = async () => {
  if (!weatherForm.value.city && !weatherForm.value.location) {
    error.value = '请输入城市名称或坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = {};
    if (weatherForm.value.city) params.city = weatherForm.value.city;
    if (weatherForm.value.location) params.location = weatherForm.value.location;
    
    const response = useMockApi.value 
      ? await getWeatherMock(params)
      : await getWeather(params);
    weatherResult.value = response;
  } catch (err) {
    console.error('天气API测试失败:', err);
    error.value = '天气API调用失败';
  } finally {
    loading.value = false;
  }
};

// 测试地理编码API
const testGeocodeApi = async () => {
  if (geocodeForm.value.type === 'geo' && !geocodeForm.value.address) {
    error.value = '请输入地址';
    return;
  }
  if (geocodeForm.value.type === 'regeo' && !geocodeForm.value.location) {
    error.value = '请输入坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = { type: geocodeForm.value.type };
    if (geocodeForm.value.address) params.address = geocodeForm.value.address;
    if (geocodeForm.value.location) params.location = geocodeForm.value.location;
    
    const response = useMockApi.value 
      ? await getGeocodeMock(params)
      : await getGeocode(params);
    geocodeResult.value = response;
  } catch (err) {
    console.error('地理编码API测试失败:', err);
    error.value = '地理编码API调用失败';
  } finally {
    loading.value = false;
  }
};

// 测试POI搜索API
const testPoiApi = async () => {
  if (!poiForm.value.location) {
    error.value = '请输入中心坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = { location: poiForm.value.location };
    if (poiForm.value.keywords) params.keywords = poiForm.value.keywords;
    if (poiForm.value.radius) params.radius = poiForm.value.radius;
    if (poiForm.value.types) params.types = poiForm.value.types;
    
    const response = useMockApi.value 
      ? await getPoiMock(params)
      : await getPoi(params);
    poiResult.value = response;
  } catch (err) {
    console.error('POI搜索API测试失败:', err);
    error.value = 'POI搜索API调用失败';
  } finally {
    loading.value = false;
  }
};

// 测试路径规划API
const testRouteApi = async () => {
  if (!routeForm.value.origin || !routeForm.value.destination) {
    error.value = '请输入起点和终点坐标';
    return;
  }
  
  loading.value = true;
  error.value = '';
  
  try {
    const params: any = {
      origin: routeForm.value.origin,
      destination: routeForm.value.destination,
      strategy: routeForm.value.strategy
    };
    if (routeForm.value.waypoints) params.waypoints = routeForm.value.waypoints;
    
    const response = useMockApi.value 
      ? await getRouteMock(params)
      : await getRoute(params);
    routeResult.value = response;
  } catch (err) {
    console.error('路径规划API测试失败:', err);
    error.value = '路径规划API调用失败';
  } finally {
    loading.value = false;
  }
};

// 快速测试函数
const quickTestWeather = () => {
  weatherForm.value.city = '北京';
  weatherForm.value.location = '';
  testWeatherApi();
};

const quickTestGeocode = () => {
  geocodeForm.value.type = 'regeo';
  geocodeForm.value.location = '116.397428,39.90923';
  geocodeForm.value.address = '';
  testGeocodeApi();
};

const quickTestPoi = () => {
  poiForm.value.location = '116.397428,39.90923';
  poiForm.value.keywords = '餐厅';
  poiForm.value.radius = 1000;
  poiForm.value.types = '';
  testPoiApi();
};

const quickTestRoute = () => {
  routeForm.value.origin = '116.397428,39.90923';
  routeForm.value.destination = '116.407428,39.91923';
  routeForm.value.strategy = 0;
  routeForm.value.waypoints = '';
  testRouteApi();
};
</script>

<style scoped>
.api-test-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.page-header {
  text-align: center;
  color: white;
  margin-bottom: 30px;
}

.page-header h1 {
  font-size: 2.5rem;
  margin-bottom: 10px;
  font-weight: 300;
}

.page-header p {
  font-size: 1.1rem;
  opacity: 0.9;
}

.api-mode-switch {
  max-width: 1200px;
  margin: 0 auto 30px;
  background: white;
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.mode-toggle {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}

.mode-btn {
  flex: 1;
  padding: 12px 20px;
  border: 2px solid #e1e5e9;
  background: #f8f9fa;
  color: #666;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.mode-btn.active {
  background: #667eea;
  color: white;
  border-color: #667eea;
}

.mode-btn:hover {
  transform: translateY(-2px);
}

.mode-info {
  text-align: center;
  font-size: 14px;
}

.mock-info {
  color: #28a745;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.real-info {
  color: #007bff;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.api-container {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  gap: 30px;
}

.api-section {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.api-section h2 {
  color: #333;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.5rem;
}

.api-section h2 i {
  color: #667eea;
}

.api-form {
  display: grid;
  gap: 15px;
  margin-bottom: 20px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.input-group label {
  font-weight: 600;
  color: #555;
  font-size: 14px;
}

.input-group input,
.input-group select {
  padding: 12px 15px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s;
}

.input-group input:focus,
.input-group select:focus {
  border-color: #667eea;
}

.test-btn {
  padding: 12px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.test-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.test-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.api-result {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  border-left: 4px solid #667eea;
}

.api-result h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.api-result pre {
  background: #2d3748;
  color: #e2e8f0;
  padding: 15px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.4;
}

.quick-test-section {
  background: white;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.quick-test-section h2 {
  color: #333;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.5rem;
}

.quick-test-section h2 i {
  color: #667eea;
}

.quick-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}

.quick-btn {
  padding: 15px 20px;
  background: #f8f9fa;
  color: #333;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.quick-btn:hover {
  background: #667eea;
  color: white;
  border-color: #667eea;
  transform: translateY(-2px);
}

.error-message {
  background: #fff5f5;
  color: #e53e3e;
  padding: 15px;
  border-radius: 8px;
  border: 1px solid #fed7d7;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  margin-top: 20px;
}

@media (max-width: 768px) {
  .page-header h1 {
    font-size: 2rem;
  }
  
  .api-container {
    gap: 20px;
  }
  
  .api-section {
    padding: 20px;
  }
  
  .quick-buttons {
    grid-template-columns: 1fr;
  }
  
  .mode-toggle {
    flex-direction: column;
  }
}
</style> 